package com.itheima.canal.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

	//注意：队列范围不能写成private，否则在监听类中就不能引用该队列。。。
	public static final String AD_UPDATE_QUEUE = "ad_update_queue"; //ctrl+shift+u 大小写转换、

	//定义队列名称 用于监控商品上下架状态变化的队列，且该队列会将消息发送到索引库 Elasticsearch中
	public static final String SEARCH_ADD_QUEUE = "search_add_queue";

	//定义交换机名称 用于发送消息到search_add_queue队列和 静态页面队列
	public static final String GOODS_UP_EXCHANGE = "goods_up_exchange";

	//商品下架交换机
	public static final String GOODS_DOWN_EXCHANGE = "goods_down_exchange";

	//搜索服务下架商品队列
	public static final String SEARCH_DEL_QUEUE = "SEARCH_DEL_QUEUE";


	//声明监听ad表的队列
	@Bean
	public Queue queue() {
		return new Queue(AD_UPDATE_QUEUE);
	}

	//申明 search_add_queue队列
	@Bean(SEARCH_ADD_QUEUE)//name和value都一样的作用，都是起别名，都可以省略
	public Queue queue2() {
		return new Queue(SEARCH_ADD_QUEUE);
	}

	//申明 goods_up_exchange交换机
	@Bean(name = GOODS_UP_EXCHANGE)//name和value都一样的作用，都是起别名，都可以省略
	public Exchange exchange() {
		return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
	}

	@Bean
	public Binding binding(@Qualifier(SEARCH_ADD_QUEUE) Queue queue, @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("").noargs();
	}


	//申明下架队列
	@Bean(SEARCH_DEL_QUEUE )
	public Queue queue1() {
		return new Queue(SEARCH_DEL_QUEUE);
	}

	//申明下架交换机
	@Bean(GOODS_DOWN_EXCHANGE )
	public Exchange exchange1() {
		return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).build();
	}

	//将下架队列绑定到下架交换机
	@Bean
	public Binding binding1(@Qualifier(SEARCH_DEL_QUEUE) Queue queue,@Qualifier(GOODS_DOWN_EXCHANGE) Exchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("").noargs();
	}
}
